<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Services;

use App\Enums\AdminActionTypeEnum;
use App\Exceptions\ApiException;
use App\Format\CoverConfigFormat;
use App\Models\CoverConfig;

class CoverConfigService
{
    public function getList(array $search)
    {
        $builder = CoverConfig::query();

        if (isset($search['name'])) {
            $builder->where('name', $search['name']);
        }

        $list = $builder->get();

        return [
            'list' => $list,
        ];
    }

    public function update(CoverConfigFormat $coverConfigFormat)
    {
        $id = $coverConfigFormat->getId();
        if (empty($id)) {
            throw new ApiException('缺少 id 参数', 1);
        }
        $coverConfigFormat->setId(null);
        CoverConfig::query()->where('id', $id)->update($coverConfigFormat->toArrayNotNull());
        adminLog(new CoverConfig(), AdminActionTypeEnum::UPDATE, '修改 ' . $coverConfigFormat->getName() . ' 封面图');
        return true;
    }
}
